import 'package:flutter/material.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import '../utils/my_common.dart';

/// 自定义加载遮罩组件
class CustomLoadingOverlay extends StatelessWidget {
  final String loadingText;
  final bool isVisible;

  const CustomLoadingOverlay({
    super.key,
    required this.loadingText,
    this.isVisible = true,
  });

  @override
  Widget build(BuildContext context) {
    if (!isVisible) return const SizedBox.shrink();

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        color: MyCommonUtils.black.withValues(alpha: 0.7),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              LoadingAnimationWidget.inkDrop(
                color: MyCommonUtils.blue,
                size: 60.w,
              ),
              SizedBox(height: 24.h),
              Text(
                loadingText,
                style: TextStyle(
                  fontSize: 16.sp,
                  color: MyCommonUtils.blue,
                  fontWeight: FontWeight.w700,
                ),
              ).center(),
            ],
          ),
        ),
      ),
    );
  }

  static void show(BuildContext context, String loadingText) {
    showGeneralDialog(
      context: context,
      barrierDismissible: false,
      barrierColor: Colors.transparent,
      transitionDuration: Duration.zero,
      pageBuilder: (context, animation, secondaryAnimation) {
        return CustomLoadingOverlay(loadingText: loadingText);
      },
    );
  }

  /// 关闭加载遮罩
  static void hide(BuildContext context) {
    Navigator.of(context, rootNavigator: true).pop();
  }
}
